<?
/**
 * Copyright 2007 Melange.
 *
 * This file is part of PHP-MELANGE.
 *
 * PHP-MELANGE is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * PHP-MELANGE is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with PHP-MELANGE; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * @category    Melange
 * @package     php-melange
 * @subpackage  cms
 * @copyright   Copyright (c) 2007 Jeroen Simons. All rights reserved
 * @author      Jeroen Simons <jeroen@melange.nl>
 * @link        http://www.melange.nl/
 *
 */

class PHP_CMS_View_Helper_List_List {

    function listr($toNew = null, $form, $rows, $fields=array()) {
        global $resources;
?>
   <div>
       <input type="hidden" name="order" value="<?=$form->getOrder()?>" />
       <input type="hidden" name="direction" value="<?=$form->getDirection()?>" />
       <input type="hidden" name="page" value="<?=$form->getPage()?>" />
       <input type="hidden" name="id" value="<?=$form->getId()?>" />
   </div>

   <div class="toplistbar">

       <? if(!is_null($toNew)) { ?>
           <input class="button" type="button" name"new" value="<?=!is_null($resources) ? $this->pageContext->message("button.new") : "New"?>" onclick="to('<?=$toNew?>')" /> |
       <? } ?>
       <?=self::page($form, $rows)?> |
       <?=self::info($form, $rows)?> |

           <?=$this->pageContext->message("label.search")?> :
           <input class="query" type="text" name="query" value="<?=$form->getQuery()?>" onkeydown="this.form.page.value=0"/>

           <?=$this->pageContext->message("label.fields")?> :
           <select name="filter" onchange="this.form.page.value=0">
               <option value="">All</option>
               <? foreach(array_keys($fields) as $key) { ?>
                   <option value="<?=$key?>" <?=$form->getFilter() == $key ? " selected=\"selected\"" : ""?>><?=$fields[$key]?></option>
               <? } ?>
           </select>

           <?=$this->pageContext->message("label.results-per-page")?>:
           <select name="pages" onchange="this.form.page.value=0">
               <option value="10"<?=$form->getPages() == "10" ? " selected=\"selected\"" : ""?>>10</option>
               <option value="20"<?=$form->getPages() == "20" ? " selected=\"selected\"" : ""?>>20</option>
               <option value="30"<?=$form->getPages() == "30" ? " selected=\"selected\"" : ""?>>30</option>
               <option value="40"<?=$form->getPages() == "40" ? " selected=\"selected\"" : ""?>>40</option>
               <option value="50"<?=$form->getPages() == "50" ? " selected=\"selected\"" : ""?>>50</option>
               <option value="100"<?=$form->getPages() == "100" ? " selected=\"selected\"" : ""?>>100</option>
               <option value=""<?=$form->getPages() == "" ? " selected=\"selected\"" : ""?>><?=$this->pageContext->message("choose.all")?></option>
           </select>

           <input class="button" type="submit" value="<?=$this->pageContext->message("button.search")?>" />

   </div><?

    }

    public static function info($form, $rows) {
        $start = $form->getPage() * $form->getPages();
        $end   = $rows < $start + $form->getPages() || $form->getPages() == "" ? $rows : $start + $form->getPages();
?>
<?=$this->pageContext->message("label.showing")?>: <?=$start + 1?>/<?=$end?> <?=$this->pageContext->message("label.showing.of")?> <?=$rows?><?
   }

   public static function page($form, $rows) {
       $pages = $form->getPages() != "" ? ceil($rows / $form->getPages()) : 1;
        $start = $form->getPage() * $form->getPages();
        $end   = $start + $rows < $start + $form->getPages() ? $start + $rows : $start + $form->getPages();
?>
<?=$this->pageContext->message("label.pages")?>: <? for($i=0; $i<$pages; $i++) { ?><?=($i != $form->getPage()) ? "<a href=\"javascript:setPage('".get_class($form)."',".$i.")\">".($i + 1)."</a>" : ($i + 1)?><?=($i < $pages - 1) ? "&nbsp;|&nbsp;" : ""?><? } ?><?
   }

}
?>
